package com.gopay.webtest.util;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.log4j.Logger;

public class SignUtil {

	private static Logger logger = Logger.getLogger(SignUtil.class);
	public static final String sign(String message, String type, String customerId,String envName)
			throws Exception {
		logger.info("Encryption Type : " + type);
		logger.info("Encrypt Plaintext : "+message);
		try {
			if ("1".equals(type)) {
				String md5Msg = md5(message);
				logger.info("MD5 Encryption message : "+md5Msg);
				return md5Msg;
			} else if ("2".equals(type)) {
				String shaMsg = sha(message);
				logger.info("SHA Encryption message : "+shaMsg);
				return shaMsg;
			} else if("3".equals(type)){
				//采用api证书来对报文进行加签
				String apiMsg = api(message,customerId,envName);
				return apiMsg;
			}else {
				String md5Msg = md5(message);
				logger.info("MD5 Encryption message : "+md5Msg);
				return md5Msg;
			}
		} catch (Exception ueex) {
			throw ueex;
		}
	}

	public static String md5(String message)
			throws UnsupportedEncodingException {
		return DigestUtils.md5Hex(message.getBytes("UTF-8"));
	}

	private static String sha(String message)
			throws UnsupportedEncodingException {
		return DigestUtils.shaHex(message.getBytes("UTF-8"));
	}

	private static String api(String message, String customerId, String envName){
		//根据证书获得私钥
		//根据当前环境名称获得不通的路径
		logger.info("所用环境 envName = " + envName);
		String filePath = ApiUtil.apicertPath;
		if(envName.contains("newplatform")||envName.contains("prod")){
			filePath = filePath + File.separator + "prod" ;
		}else if(envName.contains("gray")||envName.contains("灰度机")){
			filePath = filePath + File.separator + "gray" ;
		}else if(envName.contains("mer")||envName.contains("商户接入测试环境")){
			filePath = filePath + File.separator + "mer" ;
		}else if(envName.contains("test")){
			filePath = filePath + File.separator + "test" ;
		}else if(envName.contains("bank")){
			filePath = filePath + File.separator + "bank" ;
		}else{
			filePath = filePath + File.separator + "dev" ;
		}

		PrivateKey privateKey = ApiUtil.GetPvkformPfx(filePath+File.separator+customerId+".pfx","");
		String signStr = "";
		if(privateKey!=null){
			byte[] privateKeyBytes = ApiUtil.getPriveteKeyBytes(privateKey);
			String privateKeyStrs = ApiUtil.getPrivateKeyStrs(privateKeyBytes);
			logger.info("商户上行报文服务器证书签名");
			//现将报文md5后，在签名
			try {
				String md5Str = md5(message);
				System.out.println("原串 = "+ message);
				System.out.println("md5后 = " + md5Str);
				signStr = ApiUtil.sign(md5Str,privateKeyStrs);
				System.out.println("签名串 = " + signStr);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			logger.info("商户上行报文服务器证书签名成功");
		}

		return signStr;

	}

	/**
	 * 验签
	 * @param content
	 * @param sign
     * @return
     */
	public static boolean doCheck(String content,String sign){
		return ApiUtil.doCheck(content,sign);
	}



}